//package com.actionsoft.apps.vote.notification;
//
//import com.actionsoft.apps.resource.AppContext;
//import com.actionsoft.apps.vote.dao.VoteDao;
//import com.actionsoft.apps.vote.dao.VoteRecDao;
//import com.actionsoft.apps.vote.model.VoteModel;
//import com.actionsoft.apps.vote.model.VoteRecModel;
//import com.actionsoft.bpms.client.notification.NotificationMessageFormatter;
//import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
//import com.actionsoft.bpms.commons.security.ac.AccessControlAPI;
//import com.actionsoft.bpms.server.DispatcherRequest;
//import com.actionsoft.bpms.server.RequestContext;
//import com.actionsoft.bpms.server.UserContext;
//import com.actionsoft.bpms.util.UtilString;
//import com.actionsoft.sdk.local.SDK;
//import com.actionsoft.sdk.local.api.AppAPI;
//import com.actionsoft.sdk.local.api.PermAPI;
//import com.alibaba.fastjson.JSONObject;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//
//public class VoteFormatter
//        implements NotificationMessageFormatter {
//    @Override
//    public ResponseObject parser(UserContext user, String content) {
//        ResponseObject ro = ResponseObject.newOkResponse();
//        JSONObject json = JSONObject.parseObject(content);
//        String voteText = "我来投一票";
//
//        String voteId = json.getString("voteId");
//        AppContext appContext = SDK.getAppAPI().getAppContext("com.actionsoft.apps.vote");
//        String appName = "投票";
//        if (appContext != null) {
//            appName = appContext.getName();
//        }
//        if (!UtilString.isEmpty(voteId)) {
//            boolean isPostVote = false;
//            VoteDao dao = new VoteDao();
//            VoteRecDao voteRecDao = new VoteRecDao();
//            VoteModel model = dao.getVoteById(voteId);
//            if (model != null) {
//                VoteRecModel voteRec = new VoteRecModel();
//                voteRec.setVoteId(model.getId());
//                voteRec.setVoteuser(user.getUID());
//                int voteCounts = voteRecDao.getUserCount(voteRec);
//                if (voteCounts > 0) {
//                    isPostVote = true;
//                }
//            }
//            if (isPostVote) {
//                voteText = "查看投票结果";
//            }
//            String data = json.getString("data");
//            ro.put("content", data);
//
//            List<Map<String, String>> buttons = new ArrayList();
//            Map<String, String> button1 = new HashMap();
//            button1.put("name", voteText);
//            if (user.isMobileClient()) {
//                String userAgent = DispatcherRequest.getContext().getUserAgent();
//                if (userAgent.contains("Android")) {
//                    button1.put("action", "com.actionsoft.apps.vote.android:voteId=" + voteId + "&postVote=" + isPostVote);
//                } else if ((userAgent.contains("iPhone")) || (userAgent.contains("AWSMobilePortalPhone"))) {
//                    button1.put("action", "com.actionsoft.apps.vote.ios://voteId=" + voteId + "&postVote=" + isPostVote);
//                }
//                button1.put("target", "app");
//                button1.put("actionError", "请安装" + appName + "应用");
//            } else {
//                button1.put("action", "./w?sid=@sid&cmd=com.actionsoft.apps.vote_home&teamId=" + voteId);
//                button1.put("target", "blank");
//            }
//            button1.put("functionId", "vote");
//            button1.put("functionName", appName);
//            button1.put("color", "blue");
//            buttons.add(button1);
//            if (isPostVote) {
//                boolean hasPerm = false;
//                if (model.getVoteResult().equals("1")) {
//                    hasPerm = true;
//                } else {
//                    boolean isAc = AccessControlAPI.getInstance().getListByResource("vote.myAcTypeResult", voteId).hasNext();
//                    if (isAc) {
//                        hasPerm = SDK.getPermAPI().havingACPermission(user.getUID(), "vote.myAcTypeResult", voteId, 0, false);
//                    }
//                }
//                if (hasPerm) {
//                    ro.put("buttons", buttons);
//                }
//            } else {
//                ro.put("buttons", buttons);
//            }
//        }
//        return ro;
//    }
//
//    public String testAjaxButton(UserContext user, String type, String msg) {
//        return ResponseObject.newOkResponse().ok("申请被批准！type=" + type + ",msg=" + msg).toString();
//    }
//}
